<?php

 /**
  * @package JooS
  * @subpackage Loader
  */
  class JooS_Loader {
    /**
     * @param string $className
     * @return bool
     */
    public static function loadClass($className) {
      if (!class_exists($className)) {
        $path = self::getPath($className);

        $include_paths = explode(PATH_SEPARATOR, get_include_path());
        for ($i=0; $i<sizeof($include_paths); $i++) {
          $realPath = $include_paths[$i] . DIRECTORY_SEPARATOR . $path;

          if (file_exists($realPath)) {
            require_once($realPath);
            return class_exists($className);
          }
        }
        return false;
      }
      else
        return true;
    }

    /**
     * @param string $className
     * @param string $ext
     * @return string
     */
    public static function getPath($className, $ext = "php") {
      return str_replace("_", DIRECTORY_SEPARATOR, $className) . "." . $ext;
    }

    /**
     * @param string $className
     * @param string $ext
     * @return string
     */
    public static function getRelativePath($className, $ext = "php") {
      require_once "JooS/Config.php";

      return JooS_Config::PHPackager_Library()->path() . DIRECTORY_SEPARATOR . self::getPath($className, $ext);
    }

    /**
     * @param string $className
     * @param string $ext
     * @return string
     */
    public static function getRealPath($className, $ext = "php") {
      require_once "JooS/Config.php";

      return JooS_Config::PHPackager()->path() . DIRECTORY_SEPARATOR . self::getRelativePath($className, $ext);
    }

    /**
     * @todo security check
     * @param string $prefix
     * @param string $name
     * @param bool $ucfirst
     * @return string
     */
    public static function getClassName($prefix, $name, $ucfirst = false) {
      $path = explode("_", str_replace(".", "_", $name));
      if ($ucfirst)
        for ($i=0; $i<sizeof($path); $i++)
          $path[$i] = ucfirst(strtolower($path[$i]));

      return ($prefix ? $prefix ."_" : "") . implode("_", $path);
    }
  }
